package com.pet;

import com.corundumstudio.socketio.SocketIOServer;
import com.pet.chatingRoom.handle.UserHandler;
import com.pet.chatingRoom.handle.DoctorHandler;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

@SpringBootApplication
@MapperScan(value = "com.pet.mapper")
public class Starter {
    public static void main(String[] args) {
        SpringApplication.run(Starter.class);
    }
}

    @Component
    @Slf4j
    class SocketIOServerRunner implements CommandLineRunner, DisposableBean {

        @Autowired
        private SocketIOServer socketIOServer;
        @Autowired
        private DoctorHandler doctorHandler;
        @Autowired
        private UserHandler userHandler;

        @Override
        public void run(String... args) throws Exception {
            socketIOServer.getNamespace("/doctorHandler").addListeners(doctorHandler);
            socketIOServer.getNamespace("/userHandler").addListeners(userHandler);
            socketIOServer.start();
            log.info("SocketIOServer==============================启动成功");
        }


        @Override
        public void destroy() throws Exception {
            //如果用kill -9  这个监听是没用的，有可能会导致你服务kill掉了，但是socket服务没有kill掉
            socketIOServer.stop();
            log.info("SocketIOServer==============================关闭成功");
        }
}
